import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    username:null,
    token:null
  },
  getters:{
    getUserName(state){
      return state.username;
    },
    getUserToken(state){
      return state.token
    }
  },
  mutations: {
    updateUserData(state,loginedData){
      state.username = loginedData.username
      state.token = loginedData.token
      sessionStorage.username = state.username;
      sessionStorage.token = state.token;
    },
    clearUserData(state){
      state.username = null;
      state.token = null;
      sessionStorage.username = undefined;
      sessionStorage.token = undefined;
    }
  },
  actions: {
    logout(context){
      context.commit('clearUserData')
    },
    login(context,loginedData){
      context.commit('updateUserData',loginedData)
    },
    queryCurrentUser(){
      return {
        username:this.state.username,
        token:this.state.token
      }
    }
  },
  modules: {
  }
})

export default store